<?php
/*
 * Created on Jan 17, 2013
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
function frontend_channel_videos(){
	$build = array();
    
    $select = db_select('field_data_field_youtube_video', 'v')->extend('GloolsPagerDefault');
    
    $select->fields('v', array('entity_type', 'bundle', 'entity_id', 'revision_id', 'language', 'delta', 'field_youtube_video_input', 'field_youtube_video_video_id'));
    
    $select->condition('v.deleted', 0)
           ->orderBy('v.entity_id', 'DESC')
           ->limit(13)
           ->addTag('node_access')
           ->addMetaData('page-owner', 'products/videos');

    $videos = $select->execute()->fetchAllAssoc('field_youtube_video_video_id');
    debug_record_data(__FUNCTION__, $videos);
    if (empty($videos)) {
        drupal_set_title(t('Welcome to @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), PASS_THROUGH);
    
        $default_message = '<p>' . t('No videos has been created yet.') . '</p>';
    
        $default_links = array();
        if (_node_add_access()) {
          $default_links[] = l(t('Add new content'), 'node/add');
        }
        if (!empty($default_links)) {
          $default_message .= theme('item_list', array('items' => $default_links));
        }
    
        $build['default_message'] = array(
          '#markup' => $default_message,
          '#prefix' => '<div id="first-time">',
          '#suffix' => '</div>',
        );
        return $build;
    }
    
    foreach($videos as $youtubeKey=>$video) {
        $build[$youtubeKey] = array(
          '#prefix' => '<div class="youtube_video">',
          '#theme' => 'youtube_thumbnail',
          '#video_id' => $video->field_youtube_video_video_id,
          '#image_link' => array(
              'path' => $video->field_youtube_video_input,
              'options' => array('html' => TRUE),
          ),
          '#suffix' =>'</div>'
        );
    }
    // 'rss.xml' is a path, not a file, registered in node_menu().
    drupal_add_feed('rss.xml', variable_get('site_name', 'Drupal') . ' ' . t('RSS'));
    
    $build['pager'] = array(
      '#theme'   => 'glools_pager',
      '#weight'  => 5,
      '#element' => 'products/videos'
    );
    drupal_set_title('IKNOW videos');
    
    include_once(drupal_get_path('module', 'youtube') . '/youtube.theme.inc');
    
    drupal_add_css(path_to_theme() . '/css/channels/videos.channel.css');
    
    return $build;
}

function frontend_channel_videos_init(){
    return request_path();
}

/**
 * @return stdClass
 * - section: string
 * - data: array('options'=>array('global'=>$variables, '[private]'=>$variables))
 *         Private key values:
 *         $privateKey = array(
 *             $region.'.'.$info['view'].'.'.$info['private_key'],
 *             $region.'.'.$info['view'].'.'.$weight,
 *             $region.'.'.$info['view'],
 *             $info['view']
 *         );
 * */
function frontend_channel_videos_viewPreset(&$vars, $path){
    
    $re = new stdClass();
    $re->section = 'videos';
    $re->data = array();
    
    $re->data['options'] = array();
    
    return $re;
}

function frontend_channel_videos_page_build(){
    //frontend_attach_clientLibForChannel('videos');
    
    if (preg_match('/videos\/[0-9a-zA-Z\-\_]+/', request_path())) {
        drupal_add_css(path_to_theme() . '/css/components/comment.com.css');
    }
}

/**
 * @Description:
 * The parameter [channel or channel/category] should be matched with request_path();
 * The path scan rule is the same with menu path.
 * e.g: "a/b/c" == request_path();
 *      Client library scan order: 
 *      1. "a/b/c"==[channel or channel/category]
 *      2. "a/b"==[channel or channel/category]
 *      3. "a"==[channel or channel/category]
 * The scan process would be break when any one of the client lib settings matched or nothing matched.
 * 
 * HOOK:
 * Be invoke by frontend_attach_clientLibForChannel();
 * */
function frontend_channel_videos_clientLibSettings(){
    return array(
        '[channel or channel/category]' => array(
            // the file path is under channel base path "[theme path]/channelLib/sample/[file path][.css]|[.js]"
            'type: css|js' => 'file name prefix', // The real file name rule is "[file name prefix].channel.[js|cssj"  
        ),
        '[channel or channel/category]' => array(
            // the file path is under channel base path "[theme path]/channelLib/sample/[file path][.css]|[.js]"
            'type: css|js' => 'file name prefix', // The real file name rule is "[file name prefix].channel.[js|cssj"  
        )
    );
}